<template>
  <div class="mentor-page">
    <!-- 页面标题 -->
    <div class="page-header">
      <h1>课题浏览与申请</h1>
      <p>浏览企业发布的课题，选择感兴趣的进行申请</p>
    </div>

    <!-- 功能导航 -->
    <el-tabs v-model="activeTab" class="mentor-tabs">
      <el-tab-pane label="课题浏览" name="topics">
        <!-- 课题筛选 -->
        <div class="filter-section">
          <el-card class="filter-card">
            <div class="filter-row">
              <el-input
                v-model="topicFilters.keyword"
                placeholder="输入关键词搜索课题"
                clearable
                style="width: 300px"
              >
                <template #prefix>
                  <el-icon><Search /></el-icon>
                </template>
              </el-input>

              <el-select
                v-model="topicFilters.category"
                placeholder="选择学科"
                clearable
                style="width: 200px"
              >
                <el-option label="计算机科学" value="computer_science" />
                <el-option label="电子信息" value="electronics" />
                <el-option label="机械工程" value="mechanical" />
                <el-option label="化学化工" value="chemistry" />
                <el-option label="生物医学" value="biomedical" />
                <el-option label="经济管理" value="economics" />
                <el-option label="其他" value="other" />
              </el-select>

              <el-select
                v-model="topicFilters.status"
                placeholder="选择状态"
                clearable
                style="width: 200px"
              >
                <el-option label="开放" value="open" />
                <el-option label="已关闭" value="closed" />
                <el-option label="进行中" value="in_progress" />
                <el-option label="已完成" value="completed" />
              </el-select>

              <el-input-number
                v-model="topicFilters.budget_min"
                placeholder="最小预算"
                :min="0"
                :step="1000"
                style="width: 150px"
              />

              <el-input-number
                v-model="topicFilters.budget_max"
                placeholder="最大预算"
                :min="0"
                :step="1000"
                style="width: 150px"
              />

              <el-select
                v-model="topicFilters.is_public"
                placeholder="是否公开"
                clearable
                style="width: 150px"
              >
                <el-option label="公开" value="true" />
                <el-option label="不公开" value="false" />
              </el-select>

              <el-button type="primary" @click="handleTopicSearch">
                <el-icon><Search /></el-icon>
                搜索
              </el-button>

              <el-button @click="resetTopicFilters">
                <el-icon><Refresh /></el-icon>
                重置
              </el-button>
            </div>
          </el-card>
        </div>

        <!-- 课题列表 -->
        <div class="topics-section">
          <!-- 空状态 -->
          <div v-if="topics.length === 0" class="empty-state">
            <el-empty description="暂无课题数据">
              <el-button type="primary" @click="fetchProjects">刷新数据</el-button>
            </el-empty>
          </div>
          
          <!-- 课题列表 -->
          <el-row v-else :gutter="20">
            <el-col
              v-for="topic in topics"
              :key="topic.id"
              :xs="24"
              :sm="12"
              :md="8"
              :lg="6"
            >
              <el-card class="topic-card" shadow="hover">
                <div class="topic-header">
                  <h3 class="topic-title">{{ topic.title }}</h3>
                  <el-tag :type="getStatusType(topic.status)" size="small">
                    {{ formatStatus(topic.status) }}
                  </el-tag>
                </div>

                <div class="topic-content">
                  <p class="topic-description">{{ topic.description }}</p>

                  <div class="topic-info">
                    <div class="info-item">
                      <el-icon><OfficeBuilding /></el-icon>
                      <span>{{ topic.enterprise }}</span>
                    </div>
                    <div class="info-item">
                      <el-icon><Collection /></el-icon>
                      <span>{{ topic.category }}</span>
                    </div>
                    <div class="info-item">
                      <el-icon><Money /></el-icon>
                      <span>{{ topic.budget || '面议' }}</span>
                    </div>
                  </div>

                  <div class="topic-requirements">
                    <h4>要求：</h4>
                    <ul>
                      <li v-for="(req, index) in getRequirementsList(topic.requirements)" :key="index">{{ req }}</li>
                    </ul>
                  </div>
                </div>

                <div class="topic-actions">
                  <el-button
                    type="primary"
                    @click="showApplyDialog(topic)"
                    :disabled="hasApplied(topic.id)"
                  >
                    {{ hasApplied(topic.id) ? '已申请' : '申请课题' }}
                  </el-button>

                  <el-button @click="showTopicDetail(topic)">
                    查看详情
                  </el-button>

                  <el-button 
                    type="success" 
                    @click="contactEnterprise(topic)"
                    :icon="ChatDotRound"
                  >
                    联系企业
                  </el-button>
                </div>
              </el-card>
            </el-col>
          </el-row>

          <!-- 分页 -->
          <div class="pagination-wrapper">
            <el-pagination
              v-model:current-page="currentPage"
              v-model:page-size="pageSize"
              :page-sizes="[12, 24, 36, 48]"
              :total="totalTopics"
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </div>
      </el-tab-pane>

      <el-tab-pane label="申请进度" name="applications">
        <div class="applications-section">
          <el-card class="applications-card">
            <template #header>
              <div class="card-header">
                <span>我的申请</span>
                <el-select v-model="applicationStatus" placeholder="筛选状态" style="width: 150px">
                  <el-option label="全部" value="" />
                  <el-option label="待审核" value="pending" />
                  <el-option label="已通过" value="accepted" />
                  <el-option label="已拒绝" value="rejected" />
                </el-select>
              </div>
            </template>

            <el-table :data="filteredApplications" style="width: 100%">
              <el-table-column prop="project.title" label="课题名称" />
              <el-table-column prop="project.enterprise" label="企业名称" width="150" />
              <el-table-column prop="created_at" label="申请时间" width="120">
                <template #default="scope">
                  {{ formatDate(scope.row.created_at) }}
                </template>
              </el-table-column>
              <el-table-column prop="status" label="状态" width="100">
                <template #default="scope">
                  <el-tag :type="getApplicationStatusType(scope.row.status)">
                    {{ formatStatus(scope.row.status) }}
                  </el-tag>
                </template>
              </el-table-column>
              <el-table-column prop="review_comment" label="审核意见" />
              <el-table-column label="操作" width="120">
                <template #default="scope">
                  <el-button size="small" @click="viewApplicationDetail(scope.row)">
                    查看详情
                  </el-button>
                  <el-button
                    v-if="scope.row.status === 'pending'"
                    size="small"
                    type="danger"
                    @click="withdrawApplication(scope.row.id)"
                  >
                    撤回
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-card>
        </div>
      </el-tab-pane>

      <el-tab-pane label="我的课题" name="myProjects">
        <div class="my-projects-section">
          <el-card class="my-projects-card">
            <template #header>
              <div class="card-header">
                <span>我的课题</span>
              </div>
            </template>

            <el-row :gutter="20">
              <el-col
                v-for="project in myProjects"
                :key="project.id"
                :xs="24"
                :sm="12"
                :md="8"
              >
                <el-card class="project-card" shadow="hover">
                  <div class="project-header">
                    <h3 class="project-title">{{ project.title }}</h3>
                    <el-tag :type="getProjectStatusType(project.status)" size="small">
                      {{ project.statusText }}
                    </el-tag>
                  </div>

                  <div class="project-content">
                    <p class="project-description">{{ project.description }}</p>

                    <div class="project-info">
                      <div class="info-item">
                        <el-icon><OfficeBuilding /></el-icon>
                        <span>{{ project.enterprise }}</span>
                      </div>
                      <div class="info-item">
                        <el-icon><User /></el-icon>
                        <span>导师：{{ project.mentor }}</span>
                      </div>
                      <div class="info-item">
                        <el-icon><Calendar /></el-icon>
                        <span>开始：{{ project.startDate }}</span>
                      </div>
                      <div class="info-item">
                        <el-icon><Timer /></el-icon>
                        <span>进度：{{ project.progress }}%</span>
                      </div>
                    </div>
                  </div>

                  <div class="project-actions">
                    <el-button type="primary" @click="viewProjectDetail(project)">
                      查看详情
                    </el-button>
                  </div>
                </el-card>
              </el-col>
            </el-row>
          </el-card>
        </div>
      </el-tab-pane>
    </el-tabs>

    <!-- 申请课题对话框 -->
    <el-dialog
      v-model="applyDialogVisible"
      title="申请课题"
      width="600px"
      :before-close="handleCloseApplyDialog"
    >
      <el-form :model="applyForm" :rules="applyRules" ref="applyFormRef" label-width="100px" label-position="top">
        <el-form-item label="课题名称">
          <el-input :model-value="currentTopic?.title || ''" disabled />
        </el-form-item>

        <el-form-item label="企业名称">
          <el-input :model-value="currentTopic?.enterprise || ''" disabled />
        </el-form-item>

        <el-form-item label="申请信" prop="cover_letter">
          <el-input
            v-model="applyForm.cover_letter"
            type="textarea"
            :rows="4"
            placeholder="请简要描述你的申请动机和相关经验..."
          />
        </el-form-item>

        <el-form-item label="研究计划" prop="research_plan">
          <el-input
            v-model="applyForm.research_plan"
            type="textarea"
            :rows="4"
            placeholder="请简要描述你的研究计划..."
          />
        </el-form-item>

        <el-form-item label="申请动机" prop="motivation">
          <el-input
            v-model="applyForm.motivation"
            type="textarea"
            :rows="2"
            placeholder="请简要描述你的申请动机..."
          />
        </el-form-item>

        <el-form-item label="资质说明" prop="qualifications">
          <el-input
            v-model="applyForm.qualifications"
            type="textarea"
            :rows="2"
            placeholder="请简要描述你的相关资质..."
          />
        </el-form-item>

        <el-form-item label="预期投入时间(小时/周)" prop="expected_time">
          <el-input-number
            v-model="applyForm.expected_time"
            :min="1"
            :max="40"
            controls-position="right"
          />
        </el-form-item>

        <el-form-item label="可开始时间" prop="start_date">
          <el-date-picker
            v-model="applyForm.start_date"
            type="date"
            placeholder="选择可开始日期"
            style="width: 100%"
          />
        </el-form-item>

        <el-form-item label="可结束时间" prop="end_date">
          <el-date-picker
            v-model="applyForm.end_date"
            type="date"
            placeholder="选择可结束日期"
            style="width: 100%"
          />
        </el-form-item>

        <el-form-item label="作品集链接" prop="portfolio">
          <el-input v-model="applyForm.portfolio" placeholder="请输入作品集链接" />
        </el-form-item>

        <el-form-item label="简历链接" prop="resume">
          <el-input v-model="applyForm.resume" placeholder="请输入简历链接" />
        </el-form-item>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="applyDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitApplication">提交申请</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 课题详情对话框 -->
    <el-dialog
      v-model="topicDetailVisible"
      title="课题详情"
      width="800px"
    >
      <div v-if="currentTopic" class="topic-detail">
        <h2>{{ currentTopic.title }}</h2>
        
        <el-descriptions :column="2" border>
          <el-descriptions-item label="企业名称">{{ currentTopic.enterprise || '未知企业' }}</el-descriptions-item>
          <el-descriptions-item label="课题分类">{{ currentTopic.category }}</el-descriptions-item>
          <el-descriptions-item label="预算">{{ currentTopic.budget }}元</el-descriptions-item>
          <el-descriptions-item label="截止日期">{{ formatDate(currentTopic.deadline) }}</el-descriptions-item>
          <el-descriptions-item label="状态">
            <el-tag :type="getStatusType(currentTopic.status)">
              {{ formatStatus(currentTopic.status) }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="是否公开">
            <el-tag :type="currentTopic.is_public ? 'success' : 'warning'">
              {{ currentTopic.is_public ? '公开' : '私有' }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="浏览次数">{{ currentTopic.view_count || 0 }}</el-descriptions-item>
          <el-descriptions-item label="申请次数">{{ currentTopic.apply_count || 0 }}</el-descriptions-item>
        </el-descriptions>

        <div class="detail-section">
          <h3>课题描述</h3>
          <p>{{ currentTopic.description }}</p>
        </div>

        <div class="detail-section">
          <h3>具体要求</h3>
          <div class="requirements-list">
            <p v-for="(req, index) in getRequirementsList(currentTopic.requirements)" :key="index">
              {{ index + 1 }}. {{ req }}
            </p>
          </div>
        </div>

        <div class="detail-section" v-if="currentTopic.tags">
          <h3>标签</h3>
          <div class="tags-container">
            <el-tag v-for="tag in currentTopic.tags.split(',')" :key="tag" class="tag-item">
              {{ tag.trim() }}
            </el-tag>
          </div>
        </div>

        <div class="detail-section" v-if="currentTopic.contact_info">
          <h3>联系方式</h3>
          <p>{{ currentTopic.contact_info }}</p>
        </div>

        <div class="detail-section" v-if="currentTopic.skills">
          <h3>技能要求</h3>
          <p>{{ currentTopic.skills }}</p>
        </div>

        <div class="detail-section" v-if="currentTopic.location">
          <h3>工作地点</h3>
          <p>{{ currentTopic.location }}</p>
        </div>

        <div class="detail-section" v-if="currentTopic.duration">
          <h3>项目周期</h3>
          <p>{{ currentTopic.duration }}天</p>
        </div>
      </div>
    </el-dialog>

    <!-- 申请详情对话框 -->
    <el-dialog
      v-model="applicationDetailVisible"
      title="申请详情"
      width="700px"
    >
      <div v-if="currentApplication" class="application-detail">
        <h2>{{ currentApplication.project?.title }}</h2>

        <el-descriptions :column="2" border>
          <el-descriptions-item label="企业名称">{{ currentApplication.project?.enterprise }}</el-descriptions-item>
          <el-descriptions-item label="申请时间">{{ formatDate(currentApplication.created_at) }}</el-descriptions-item>
          <el-descriptions-item label="申请状态">
            <el-tag :type="getApplicationStatusType(currentApplication.status)">
              {{ formatStatus(currentApplication.status) }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="审核时间" v-if="currentApplication.review_date">
            {{ formatDate(currentApplication.review_date) }}
          </el-descriptions-item>
        </el-descriptions>

        <div class="detail-section">
          <h3>申请信</h3>
          <p>{{ currentApplication.cover_letter }}</p>
        </div>

        <div class="detail-section">
          <h3>研究计划</h3>
          <p>{{ currentApplication.research_plan }}</p>
        </div>

        <div class="detail-section">
          <h3>简历链接</h3>
          <a :href="currentApplication.resume" target="_blank">{{ currentApplication.resume }}</a>
        </div>

        <div class="detail-section">
          <h3>作品集链接</h3>
          <a :href="currentApplication.portfolio" target="_blank" v-if="currentApplication.portfolio">
            {{ currentApplication.portfolio }}
          </a>
          <span v-else>无</span>
        </div>

        <div class="detail-section" v-if="currentApplication.review_comment">
          <h3>审核意见</h3>
          <p>{{ currentApplication.review_comment }}</p>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { onMounted } from 'vue';
import { Search, Refresh, OfficeBuilding, Collection, Calendar, Money, User, Timer, ChatDotRound } from '@element-plus/icons-vue';
import {
  handleCloseApplyDialog,
  filteredApplications,
  activeTab,
  topicFilters,
  applicationStatus,
  currentPage,
  pageSize,
  totalTopics,
  topics,
  applications,
  myProjects,
  applyDialogVisible,
  applicationDetailVisible,
  topicDetailVisible,
  currentTopic,
  currentApplication,
  applyForm,
  getStatusType,
  getApplicationStatusType,
  getProjectStatusType,
  formatStatus,
  formatDate,
  hasApplied,
  handleTopicSearch,
  resetTopicFilters,
  showApplyDialog,
  showTopicDetail,
  contactEnterprise,
  submitApplication,
  viewApplicationDetail,
  withdrawApplication,
  viewProjectDetail,
  handleSizeChange,
  handleCurrentChange,
  applyRules,
  getRequirementsList,
  fetchProjects,
  fetchApplications,
  fetchMyProjects
} from '@/api/mentor';

// 组件挂载时初始化数据
onMounted(() => {
  fetchProjects();
});

// 其他组件特有的逻辑（如果有）可以保留
</script>

<style scoped>
.mentor-page {
  padding: 20px;
}

.page-header {
  margin-bottom: 24px;
  text-align: center;
}

.page-header h1 {
  color: #045108;
  margin-bottom: 8px;
}

.page-header p {
  color: #666;
  font-size: 16px;
}

.mentor-tabs {
  margin-bottom: 24px;
}

.filter-section {
  margin-bottom: 24px;
}

.filter-card {
  background: linear-gradient(135deg, #f8f9fa 0%, #e8f5e8 100%);
}

.filter-row {
  display: flex;
  gap: 16px;
  align-items: center;
  flex-wrap: wrap;
}

.topics-section {
  margin-bottom: 24px;
}

.topic-card {
  margin-bottom: 20px;
  transition: all 0.3s ease;
}

.topic-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
}

.topic-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 16px;
}

.topic-title {
  margin: 0;
  font-size: 18px;
  color: #045108;
  flex: 1;
  margin-right: 12px;
}

.topic-content {
  margin-bottom: 20px;
}

.topic-description {
  color: #666;
  margin-bottom: 16px;
  line-height: 1.6;
}

.topic-info {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 16px;
}

.info-item {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #666;
  font-size: 14px;
}

.topic-requirements {
  margin-bottom: 16px;
}

.topic-requirements h4 {
  margin: 0 0 8px 0;
  color: #045108;
  font-size: 14px;
}

.topic-requirements ul {
  margin: 0;
  padding-left: 20px;
  color: #666;
  font-size: 14px;
}

.topic-requirements li {
  margin-bottom: 4px;
}

.topic-actions {
  display: flex;
  gap: 12px;
}

.pagination-wrapper {
  display: flex;
  justify-content: center;
  margin-top: 32px;
}

.empty-state {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 300px;
  margin: 40px 0;
}

.topic-detail {
  .detail-section {
    margin-bottom: 24px;
    
    h3 {
      color: #303133;
      font-size: 16px;
      font-weight: 600;
      margin-bottom: 12px;
      border-bottom: 2px solid #409eff;
      padding-bottom: 8px;
    }
    
    p {
      color: #606266;
      line-height: 1.6;
      margin: 0;
    }
  }
  
  .requirements-list {
    p {
      margin-bottom: 8px;
      padding-left: 16px;
      position: relative;
      
      &::before {
        content: '';
        position: absolute;
        left: 0;
        top: 8px;
        width: 6px;
        height: 6px;
        background-color: #409eff;
        border-radius: 50%;
      }
    }
  }
  
  .tags-container {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    
    .tag-item {
      margin: 0;
    }
  }
}

.applications-section,
.my-projects-section {
  margin-bottom: 24px;
}

.applications-card,
.my-projects-card {
  background: white;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.application-detail h2 {
  color: #045108;
  margin-bottom: 16px;
}

.detail-section {
  margin-bottom: 24px;
}

.detail-section h3 {
  color: #045108;
  margin-bottom: 12px;
  font-size: 16px;
}

.detail-section p {
  color: #666;
  line-height: 1.6;
  margin: 0;
}

.detail-section a {
  color: #409eff;
  text-decoration: none;
}

.detail-section a:hover {
  text-decoration: underline;
}

.project-card {
  margin-bottom: 20px;
  transition: all 0.3s ease;
}

.project-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
}

.project-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 16px;
}

.project-title {
  margin: 0;
  font-size: 18px;
  color: #045108;
  flex: 1;
  margin-right: 12px;
}

.project-content {
  margin-bottom: 20px;
}

.project-description {
  color: #666;
  margin-bottom: 16px;
  line-height: 1.6;
}

.project-info {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 16px;
}

.project-actions {
  display: flex;
  gap: 12px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .filter-row {
    flex-direction: column;
    align-items: stretch;
  }

  .filter-row .el-input,
  .filter-row .el-select {
    width: 100% !important;
  }

  .topic-actions {
    flex-direction: column;
  }

  .topic-actions .el-button {
    width: 100%;
  }

  .card-header {
    flex-direction: column;
    gap: 12px;
    align-items: stretch;
  }
}
</style>
